package com.hnkywl.sts.action.report;

import com.hnkywl.sts.entity.organ.Ejzh;
import com.hnkywl.sts.service.organ.GsjgService;
import com.hnkywl.sts.service.report.WlycwsfcService;
import com.hnkywl.sts.service.report.ZcfzbService;
import com.hnkywl.sts.service.sys.YgService;
import net.ruixin.web.struts2.SimpleActionSupport;
import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 13-6-14
 * Time: 下午4:48
 * To change this template use File | Settings | File Templates.
 */
@Namespace("/report/zcfzb")
public class ZcfzbAction extends SimpleActionSupport {
    private Long gsId;
    private Date zcfzbStartDate = new Date();
    private Date zcfzbEndDate = new Date();
    private Date date;
    private int page;
    private int rows;
    private Long type;

    private ZcfzbService zcfzbService;
    private YgService ygService;


    @Action("zcfzb")
    public String cwsfc() {
        date = new Date();
        gsId =  ygService.findYgDetail(YgService.getCurrentYg().getId()).getZggs().getId();
        return SUCCESS;
    }

    @Action("zcfzbList")
    public void cwsfcList() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        if(gsId==null){
            gsId =  ygService.findYgDetail(YgService.getCurrentYg().getId()).getZggs().getId();
        }
        if(this.zcfzbService.checkZzcfzbJz(zcfzbStartDate,gsId)){
            renderJson("[]");
//            throw new RuntimeException("资产负债表在"+simpleDateFormat.format(zcfzbStartDate)+"前一天没有日结");
        }else {
            zcfzbEndDate = getNextDay(zcfzbEndDate);
            String json = zcfzbService.zcfzbList(gsId, zcfzbStartDate, zcfzbEndDate, page, rows);
            renderJson(json);
        }
    }

    @Action("zcfzb_detail")
    public String ejzhyemx(){

        return SUCCESS;
    }

    @Action("zcfzbmxList")
    public void zcfzbmxList(){
        zcfzbEndDate = getNextDay(zcfzbEndDate);
        renderJson(zcfzbService.findzcfzbmxList(type,gsId, zcfzbStartDate, zcfzbEndDate, page, rows));
    }


    @Action("cbjz_zcfzb")
    public void cbjz() {
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Date strat = new Date();
            Date endDate = getNextDay(strat);
            if(gsId==null){
                gsId =  ygService.findYgDetail(YgService.getCurrentYg().getId()).getZggs().getId();
            }
            if(this.zcfzbService.checkZzcfzbJz(strat,gsId)){
                throw new RuntimeException("资产负债表在"+simpleDateFormat.format(strat)+"前一天没有日结");
            }
            String errorMsg = zcfzbService.cbjz(gsId, strat, endDate);
            if (!StringUtils.isEmpty(errorMsg) && errorMsg.equals("操作成功"))
                renderJson("{\"statusCode\":\"200\", \"message\":\"操作成功!\"}");
            else
                renderJson("{\"statusCode\":\"300\", \"message\":\"" + errorMsg + "\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderJson("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    public static Date getNextDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, 1);
        date = calendar.getTime();
        return date;
    }


    public Long getGsId() {
        return gsId;
    }

    public void setGsId(Long gsId) {
        this.gsId = gsId;
    }



    public Date getZcfzbEndDate() {
        return zcfzbEndDate;
    }

    public void setZcfzbEndDate(Date zcfzbEndDate) {
        this.zcfzbEndDate = zcfzbEndDate;
    }

    public Date getZcfzbStartDate() {
        return zcfzbStartDate;
    }

    public void setZcfzbStartDate(Date zcfzbStartDate) {
        this.zcfzbStartDate = zcfzbStartDate;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getRows() {
        return rows;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }

    public Long getType() {
        return type;
    }

    public void setType(Long type) {
        this.type = type;
    }

    @Autowired
    public void setZcfzbService(ZcfzbService zcfzbService) {
        this.zcfzbService = zcfzbService;
    }
    @Autowired
    public void setYgService(YgService ygService) {
        this.ygService = ygService;
    }
}
